package com.jie.jie.mvp;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.jaydenxiao.common.commonutils.LogUtils;
import com.jie.jie.R;
import com.jie.jie.mvp.model.MeiziBean;
import com.jie.jie.mvp.util.GankService;
import com.jie.jie.mvp.util.RetrofitManager;

import java.util.ArrayList;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class TestActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static android.content.Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        context = this;
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RetrofitManager
                        .getInstance()
                        .createService(GankService.class)
                        .getMeizi(1)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<String>() {
                            @Override
                            public void call(String s) {
                                ArrayList<MeiziBean> mmLabelImgUrl = getMMLabelImgUrl(s);
                                LogUtils.loge(String.valueOf(getMMHomeImgUrl(s).size()));
                            }
                        });

                loadData();
//
//                LogUtils.loge(getMeizhi51DetailImgUrl(s));
//                ArrayList<MeiziBean> mmHomeImgUrl = getMMHomeImgUrl(s);
//                ArrayList<MeiziBean> mmLabelImgUrl = getMMLabelImgUrl(s);
//                LogUtils.loge(String.valueOf(getMMHomeImgUrl(s).size()));
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
    public static ArrayList<MeiziBean> getMMLabelImgUrl(String json){
        ArrayList<MeiziBean> beans = new ArrayList<>();
        Log.d("getMMLabelImgUrl","start");
        try{
            String[] imgs = json.split("<li");
            for(String img : imgs){
//                Log.d("getMMLabelImgUrl",img);
                if(!img.contains("_blank"))
                    continue;
                MeiziBean bean = new MeiziBean();
                img = img.substring(img.indexOf("href=\"")+6);
                bean.page = bean.from = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("src=\"")+5);
                bean.url = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("alt=\"")+5);
                bean.title = img.substring(0,img.indexOf("\""))+"\n";
                img = img.substring(img.indexOf("<i>")+3);
                bean.title += img.substring(0,img.indexOf("</i>"));
                Log.d("getMMLabelImgUrl",bean.toString());
                beans.add(bean);
            }
        }catch (Exception e){
            Log.d("getMMLabelImgUrl",e.getMessage());
        }
        return beans;
    }
    public static ArrayList<MeiziBean> getMMHomeImgUrl(String json){
        ArrayList<MeiziBean> beans = new ArrayList<>();
        Log.d("getMMHomeImgUrl","start");
        try{
            json = json.substring(json.indexOf("<ul>"),json.indexOf("</ul>"));
            String[] imgs = json.split("<li");
            for(String img : imgs){
//                Log.d("getMMHomeImgUrl",img);
                if(!img.contains("_blank"))
                    continue;
                MeiziBean bean = new MeiziBean();
                img = img.substring(img.indexOf("href=\"")+6);
                bean.page = bean.from = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("src=\"")+5);
                bean.url = img.substring(0,img.indexOf("\""));
                img = img.substring(img.indexOf("alt=\"")+5);
                bean.title = img.substring(0,img.indexOf("\""));
                Log.d("getMMHomeImgUrl",bean.toString());
                beans.add(bean);
            }
        }catch (Exception e){
            Log.d("getMMHomeImgUrl",e.getMessage());
        }
        return beans;
    }


    public static String getMeizhi51DetailImgUrl(String json){
        String url = "";
        try{
            json = json.substring(json.indexOf("bigImg"));
            json = json.substring(json.indexOf("src=\"")+5);
            url = json.substring(0,json.indexOf("\""));
            Log.d("getMeizhi51DetailImgUrl",url);
        }catch (Exception e){
            Log.d("getMeizhi51DetailImgUrl",e.getMessage());
        }
        return url;
    }

    public static void loadData() {
        final String type = "福利";
        final int pagesize = 10;
        final int page = 1;
        RetrofitManager
                .getInstance()
                .createService(GankService.class)
                .getGankData(type, pagesize, page)
                .subscribeOn(Schedulers.io())
                .observeOn(Schedulers.newThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        LogUtils.loge(s);
                    }
                });


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
